﻿
// SteamView.cpp: CSteamView 类的实现
//

#include "pch.h"
#include "framework.h"
// SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的
// ATL 项目中进行定义，并允许与该项目共享文档代码。
#ifndef SHARED_HANDLERS
#include "Steam.h"
#endif

#include "SteamDoc.h"
#include "SteamView.h"
#include "SteamDlg.h"
#include "Pipe.h"
//#include "Water.h"
#include "PipeFunc.h"
#include "PropertyData.h"
#include "refprop1.h"
//#include "windows.h"
#include "ComponentDlg.h"
#include "CompositionDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CSteamView

IMPLEMENT_DYNCREATE(CSteamView, CFormView)

BEGIN_MESSAGE_MAP(CSteamView, CFormView)
	ON_WM_CONTEXTMENU()
	ON_WM_RBUTTONUP()
	ON_WM_LBUTTONUP()
	ON_BN_CLICKED(IDC_PRECALC_BUTTON, &CSteamView::OnBnClickedPrecalcButton)
	ON_BN_CLICKED(IDC_ROUGHDLG_BUTTON, &CSteamView::OnBnClickedRoughdlgButton)
	ON_BN_CLICKED(IDC_LOCALDLG_BUTTON, &CSteamView::OnBnClickedLocaldlgButton)
	ON_BN_CLICKED(IDC_INSULDLG_BUTTON, &CSteamView::OnBnClickedInsuldlgButton)
	ON_BN_CLICKED(IDC_BLACKNESS_BUTTON, &CSteamView::OnBnClickedBlacknessButton)
	ON_BN_CLICKED(IDC_ENVIRONMENTDLG_BUTTON, &CSteamView::OnBnClickedEnvironmentdlgButton)
	ON_BN_CLICKED(IDC_ALLOWSTRESSDLG_BUTTON, &CSteamView::OnBnClickedPmpdlgButton)
	ON_BN_CLICKED(IDC_WELDINGDLG_BUTTON2, &CSteamView::OnBnClickedWeldingdlgButton2)
	ON_EN_CHANGE(IDC_T_EDIT, &CSteamView::OnEnChangeTEdit)
	ON_EN_CHANGE(IDC_TD_EDIT, &CSteamView::OnEnChangeTdEdit)
	ON_COMMAND(ID_COMPONENT, &CSteamView::OnComponent)
	ON_COMMAND(ID_COMPOSITION, &CSteamView::OnComposition)
END_MESSAGE_MAP()

// CSteamView 构造/析构

CSteamView::CSteamView() noexcept
	: CFormView(IDD_STEAM_FORM)
{
	// TODO: 在此处添加构造代码
	pipe = new CPipe();
}

CSteamView::~CSteamView()
{
}

void CSteamView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_T_EDIT, pipe->t);
	DDX_Text(pDX, IDC_P_EDIT, pipe->p);
	DDX_Text(pDX, IDC_FLOWRATE_EDIT, pipe->flowRate);
	DDX_Text(pDX, IDC_PREV_EDIT, pipe->preV);
	//DDX_Text(pDX, IDC_PREDIM_EDIT, pipe->preDim);
	DDX_Text(pDX, IDC_TD_EDIT, pipe->td);
	DDX_Text(pDX, IDC_PD_EDIT, pipe->pd);
	DDX_Text(pDX, IDC_DI_EDIT, pipe->di);
	DDX_Text(pDX, IDC_DOUT_EDIT, pipe->dout);
	DDX_Text(pDX, IDC_LENGTH_EDIT, pipe->length);
	DDX_Text(pDX, IDC_ROUGH_EDIT, pipe->rough);
	DDX_Text(pDX, IDC_LOCALCOEF_EDIT, pipe->localCoef);
	DDX_Text(pDX, IDC_INSULNAME_EDIT, pipe->insulName);
	DDX_Text(pDX, IDC_INSULTHK_EDIT, pipe->insulThk);
	DDX_Text(pDX, IDC_BLACKNESS_EDIT, pipe->blackness);
	DDX_Text(pDX, IDC_BRACKETNUM_EDIT, pipe->nBracketNum);
	DDX_Text(pDX, IDC_BRACKETLENGTH_EDIT, pipe->bracketLength);
	DDX_Text(pDX, IDC_BRACKETHEIGHT_EDIT, pipe->bracketHeight);
	DDX_Text(pDX, IDC_TA_EDIT, pipe->ta);
	DDX_Text(pDX, IDC_WINDV_EDIT, pipe->windV);
	DDX_Text(pDX, IDC_ALLOWSTRESS_EDIT, pipe->allowStress);
	DDX_Text(pDX, IDC_WELDINGCOEF_EDIT, pipe->weldingCoef);
	DDX_Text(pDX, IDC_C1_EDIT, pipe->c1);
	DDX_Text(pDX, IDC_C2_EDIT, pipe->c2);
	DDX_Text(pDX, IDC_C3_EDIT, pipe->c3);
}

BOOL CSteamView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: 在此处通过修改
	//  CREATESTRUCT cs 来修改窗口类或样式
	
	return CFormView::PreCreateWindow(cs);
}

void CSteamView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();

}

void CSteamView::OnRButtonUp(UINT /* nFlags */, CPoint point)
{
	ClientToScreen(&point);
	OnContextMenu(this, point);
}

void CSteamView::OnContextMenu(CWnd* /* pWnd */, CPoint point)
{
#ifndef SHARED_HANDLERS
	theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
#endif
}


// CSteamView 诊断

#ifdef _DEBUG
void CSteamView::AssertValid() const
{
	CFormView::AssertValid();
}

void CSteamView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

CSteamDoc* CSteamView::GetDocument() const // 非调试版本是内联的
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSteamDoc)));
	return (CSteamDoc*)m_pDocument;
}
#endif //_DEBUG


// CSteamView 消息处理程序


void CSteamView::OnBnClickedPrecalcButton()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
	if (fabs(pipe->preV) < 1e-6)
	{
		MessageBox(L"流速不可为0");
		return;
	}
	
	HINSTANCE RefpropdllInstance;
	RefpropdllInstance = LoadLibrary(CA2T(GetFullFileName("./prop.dll")));

	if (RefpropdllInstance == NULL)
	{
		AfxMessageBox(L"找不到prop.dll，请重新安装。");
		return;
	}
	fp_SETPATHdllTYPE SETPATHdll = (fp_SETPATHdllTYPE)GetProcAddress(RefpropdllInstance, "SETPATHdll");
	fp_SETUPdllTYPE SETUPdll = (fp_SETUPdllTYPE)GetProcAddress(RefpropdllInstance, "SETUPdll");
	fp_TPFLSHdllTYPE TPFLSHdll = (fp_TPFLSHdllTYPE)GetProcAddress(RefpropdllInstance, "TPFLSHdll");
	fp_WMOLdllTYPE WMOLdll = (fp_WMOLdllTYPE)GetProcAddress(RefpropdllInstance, "WMOLdll");
		
	long ierr = 0;
	char hf[refpropcharlength * ncmax], hrf[lengthofreference + 1],
		herr[errormessagelength + 1], hfmix[refpropcharlength + 1];
	char hpth[refpropcharlength];
	strcpy_s(hpth, sizeof hpth / sizeof hpth[0], GetExePath());
	SETPATHdll(hpth, refpropcharlength);
			
	for (int i = 0; i < pipe->ncomp; i++)
	{
		if (i == 0)
		{
			strcpy_s(hf, sizeof hf / sizeof hf[0], pipe->components[i]);
		}
		else
		{
			strcat_s(hf, sizeof hf / sizeof hf[0], "|");
			strcat_s(hf, sizeof hf / sizeof hf[0], pipe->components[i]);
		}
		//strcat_s(hf, sizeof hf / sizeof hf[0], ".FLD");
	}
	//AfxMessageBox(CA2T(hf));
	strcpy_s(hfmix, sizeof hfmix / sizeof hfmix[0], "hmx.bnc");
	strcpy_s(hrf, sizeof hrf / sizeof hrf[0], "DEF");
	strcpy_s(herr, sizeof herr / sizeof herr[0], "Ok");
	SETUPdll(pipe->ncomp, hf, hfmix, hrf, ierr, herr, refpropcharlength*ncmax, refpropcharlength,
		lengthofreference, errormessagelength);
	if (ierr != 0)
	{
		AfxMessageBox(CA2T(herr));
		return;
	}

	double t, p, d, dl, dv, q, e, h, s, cv, cp, w,  xliq[ncmax], xvap[ncmax];
	t = pipe->t + 273.15;
	p = pipe->p * 1000 + 101.325;
	//x[0] = 1;
	TPFLSHdll(t, p, pipe->x, d, dl, dv, xliq, xvap, q, e, h, s, cv, cp, w, ierr, herr, errormessagelength);
	if (ierr != 0)
	{
		AfxMessageBox(CA2T(herr));
		return;
	}

	double M;
	WMOLdll(pipe->x, M);
	double dmass = d * M;
	double dim = Di_Velocity(pipe->flowRate, dmass, pipe->preV);
	CString sBuf;
	sBuf.Format(_T("%.2f"), dim);
	SetDlgItemText(IDC_PREDIM_EDIT, sBuf);
		
		FreeLibrary(RefpropdllInstance);
}


void CSteamView::OnBnClickedRoughdlgButton()
{
	// TODO: 在此添加控件通知处理程序代码
	CRoughDlg roughDlg(pipe->nRoughId, pipe->rough);
	//INT_PTR nRes = roughDlg.DoModal();
	if (IDCANCEL == roughDlg.DoModal())
	{
		return;
	}

	UpdateData();

	pipe->nRoughId = roughDlg.nSelect;
	pipe->rough = roughDlg.rough;
	
	UpdateData(FALSE);
}


void CSteamView::OnBnClickedLocaldlgButton()
{
	// TODO: 在此添加控件通知处理程序代码
	CLocalDlg localDlg(pipe->localIdList, pipe->localNumList, pipe->localKList);
	if (IDCANCEL == localDlg.DoModal())
	{
		return;
	}

	UpdateData();

	pipe->localIdList = localDlg.localIndexList;
	pipe->localNumList = localDlg.localNumList;
	pipe->localKList = localDlg.localKList;
	pipe->localCoef = localDlg.kSum;

	UpdateData(FALSE);
}


void CSteamView::OnBnClickedInsuldlgButton()
{
	// TODO: 在此添加控件通知处理程序代码
	CInsulDlg insulDlg;
	if (IDCANCEL == insulDlg.DoModal())
	{
		return;
	}

	UpdateData();

	pipe->insulName = insulDlg.sName;
	pipe->nInsulId = insulDlg.nId;

	UpdateData(FALSE);
}


void CSteamView::OnBnClickedBlacknessButton()
{
	// TODO: 在此添加控件通知处理程序代码

	CBlacknessDlg blacknessDlg(pipe->nShellId, pipe->blackness);
	if (IDCANCEL == blacknessDlg.DoModal())
	{
		return;
	}

	UpdateData();

	pipe->nShellId = blacknessDlg.nSelect;
	pipe->blackness = blacknessDlg.blackness;

	UpdateData(FALSE);
}


void CSteamView::OnBnClickedEnvironmentdlgButton()
{
	// TODO: 在此添加控件通知处理程序代码
	CEnvironmentDlg environmentDlg(pipe->city, pipe->nPlaceId);
	if (IDCANCEL == environmentDlg.DoModal())
	{
		return;
	}

	pipe->city = environmentDlg.sCity;
	pipe->nPlaceId = environmentDlg.nPlace;

	int nLen = WideCharToMultiByte(CP_ACP, 0, pipe->city, -1, NULL, 0, NULL, NULL);
	char* chCity = new char[nLen];
	WideCharToMultiByte(CP_ACP, 0, pipe->city, -1, chCity, nLen, NULL, NULL);

	SetCurrentCtyw(chCity);
	delete[] chCity;

	UpdateData();

	pipe->ta = Ta(0, pipe->nPlaceId, pipe->t);
	pipe->windV = SpeedOfWind(pipe->nPlaceId);

	UpdateData(FALSE);
}


void CSteamView::OnBnClickedPmpdlgButton()
{
	// TODO: 在此添加控件通知处理程序代码
	CPmpDlg pmpDlg;
	if (IDCANCEL == pmpDlg.DoModal())
	{
		return;
	}

	UpdateData();

	pipe->nPipeId = pmpDlg.nId;
	SetCurrentPmp(pipe->nPipeId);
	pipe->allowStress = AllowStress(pipe->td);

	UpdateData(FALSE);
}


void CSteamView::OnBnClickedWeldingdlgButton2()
{
	// TODO: 在此添加控件通知处理程序代码
	if (pPmp == NULL)
	{
		MessageBox(L"先选择管道材料.");
		return;
	}
	CWeldingDlg weldingDlg(pipe->nWeldingType, pipe->weldingRTPercent);
	if (IDCANCEL == weldingDlg.DoModal())
	{
		return;
	}

	pipe->nWeldingType = weldingDlg.nWeldingType;
	pipe->weldingRTPercent = weldingDlg.RTPercent / 100;

	UpdateData();

	pipe->weldingCoef = WeldingCoef(pipe->nWeldingType, pipe->weldingRTPercent);

	UpdateData(FALSE);
}


void CSteamView::OnEnChangeTEdit()
{
	// TODO:  如果该控件是 RICHEDIT 控件，它将不
	// 发送此通知，除非重写 CFormView::OnInitDialog()
	// 函数并调用 CRichEditCtrl().SetEventMask()，
	// 同时将 ENM_CHANGE 标志“或”运算到掩码中。

	// TODO:  在此添加控件通知处理程序代码
	
	UpdateData();

	if (pipe->nPlaceId == 2)
	{
		pipe->ta = Ta(0, pipe->nPlaceId, pipe->t);
	}

	UpdateData(FALSE);
}


void CSteamView::OnEnChangeTdEdit()
{
	// TODO:  如果该控件是 RICHEDIT 控件，它将不
	// 发送此通知，除非重写 CFormView::OnInitDialog()
	// 函数并调用 CRichEditCtrl().SetEventMask()，
	// 同时将 ENM_CHANGE 标志“或”运算到掩码中。

	// TODO:  在此添加控件通知处理程序代码

	UpdateData();

	if (pPmp != NULL)
	{
		pipe->allowStress = AllowStress(pipe->td);
	}

	UpdateData(FALSE);
}


void CSteamView::OnComponent()
{
	// TODO: 在此添加命令处理程序代码
	
	CComponentDlg componentDlg(pipe->components, pipe->ncomp);
	if (IDCANCEL == componentDlg.DoModal())
	{
		return;
	}
	pipe->ncomp = componentDlg.ncomp;
	for (int i = 0; i < pipe->ncomp; i++)
	{
		strcpy_s(pipe->components[i], sizeof  pipe->components[i] / sizeof  pipe->components[i][0], componentDlg.components[i]);
	}
	if (componentDlg.isModified)
	{
		for (int i = 0; i < pipe->ncomp; i++)
		{
			pipe->x[i] = 0;
		}
	}

	CCompositionDlg compositionDlg(pipe->components, pipe->x, pipe->ncomp);
	if (IDCANCEL == compositionDlg.DoModal())
	{
		return;
	}
	for (int i = 0; i < pipe->ncomp; i++)
	{
		pipe->x[i] = compositionDlg.x[i];
	}
	//AfxMessageBox(compositionDlg.str);
}


void CSteamView::OnComposition()
{
	// TODO: 在此添加命令处理程序代码
	if (0 == pipe->ncomp)
	{
		AfxMessageBox(L"未选择组分");
		return;
	}
	CCompositionDlg compositionDlg(pipe->components, pipe->x, pipe->ncomp);
	if (IDCANCEL == compositionDlg.DoModal())
	{
		return;
	}
	for (int i = 0; i < pipe->ncomp; i++)
	{
		pipe->x[i] = compositionDlg.x[i];
	}
}
